
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;



public class PanelSuperior extends JPanel{

	
	private static final long serialVersionUID = 4L;

	private ServidorBaseDatos baseDatos 		;	
	private JButton botonConectarse 			= new JButton("Conectarse");
	private JLabel nombreUsuario 				= new JLabel();
	private JLabel nombreEmpresa				= new JLabel("Carballes propiedades");
	private VentanaConexionBaseDatos ventanaconexion 	;
				
	
	public PanelSuperior(ServidorBaseDatos baseDatos){
	
		this.baseDatos=baseDatos;
		ventanaconexion = new VentanaConexionBaseDatos(baseDatos,this);
		
		this.setLayout(new GridBagLayout());
		agregarBorde();
		inicializar();
		agregarNombreEmpresa();
		agregarNombreUsuario();
		agregarBotonconexion();
		
		ventanaconexion.setLocationRelativeTo(null);
	}
	private void agregarBorde(){
		
		Border raisedbevel, loweredbevel;
		raisedbevel = BorderFactory.createRaisedBevelBorder();
		loweredbevel = BorderFactory.createLoweredBevelBorder();
		Border compound;
		compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
		this.setBorder(compound);
	}
	private void inicializar() {
		
		ActionListener eventoconexion = new ActionListener(){ 
			public void actionPerformed(ActionEvent e){
				if(botonConectarse.getText().equals("Conectarse")){
					ventanaconexion.setVisible(true);
				}else{
					botonConectarse.setText("Conectarse");
					nombreUsuario.setText("");
					baseDatos.cerrarConexion();
					PanelIzquierdo.cambiarPanelDerecho();
				}
			}
		};
		botonConectarse.addActionListener(eventoconexion);
	}
	private void agregarBotonconexion(){
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 3;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.gridheight = 3;
		constraints.insets = new Insets(0, 0, 0, 20);	//espacio contra el borde izquierda
		this.add (botonConectarse, constraints);
	}
	private void agregarNombreEmpresa(){
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.gridheight = 3;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weightx = 1;
		constraints.weighty = 1;	
		constraints.anchor = GridBagConstraints.WEST;
		constraints.insets = new Insets(0, 30, 0, 0);	//espacio contra el borde izquierda
		this.add (nombreEmpresa, constraints);
	}
	private void agregarNombreUsuario(){
		
		GridBagConstraints constraints = new GridBagConstraints();	
		constraints.gridx = 2;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.gridheight = 3;
		constraints.anchor = GridBagConstraints.EAST;
		constraints.weightx = 1.0;
		constraints.insets = new Insets(0, 30, 0, 10);	//espacio contra el borde izquierda
		this.add (nombreUsuario, constraints);
	}
	public void setEstadoConexion(boolean conectado){
		
		if(conectado){
			botonConectarse.setText("Desconectarse");
			nombreUsuario.setText("Usuario: " + baseDatos.getServidorUsuario());
		}else{
			botonConectarse.setText("Conectarse");
			nombreUsuario.setText("");
		}
	}

}
